# SYNOPSIS
#
#   CHECK_EXAMPLES
#
# DESCRIPTION
#
#   Detect libraries needed to run the different
#   the objects to be build are provided here
#   to facilitate conditional compilation of the program
#   package with the Awa application
#   Defines HAVE_NCURSES_H when ncurses is found.
#           NCURSES_LIB    path to ncurses libraries
#           NCURSES_H      path to header files
#           NCURSES_OBJ    name of example objects to build
#	    NCURSES_SRC    source code involved
#
#   Defines HAVE_GTKMM_H   when gtkmm is found
#           GTKMM_LIB      path to gtkmm libraries
#           GTKMM_H        path to gtkmm header files
#           GTKMM_OBJ      name of example objects to build
#	    GTKMM_SRC      source code involved
#
#   Defines HAVE_QT_H      when qt is found  
#           QT_LIB         path to Qt libraries
#           QT_H           path to qt header files
#           QT_OBJ         name of example object to build
#	    QT_SRC	   source code involved
#
# COPYLEFT
#
#   Copyright (c) 2008 Mamadou Gueye <mamadou77@gmail.com>
#
#   This program is free software: you can redistribute it and/or
#   modify it under the terms of the GNU General Public License as
#   published by the Free Software Foundation, either version 3 of the
#   License, or (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful, but
#   WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
#   General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program. If not, see
#   <http://www.gnu.org/licenses/>.
#
#   As a special exception, the respective Autoconf Macro's copyright
#   owner gives unlimited permission to copy, distribute and modify the
#   configure scripts that are the output of Autoconf when processing
#   the Macro. You need not follow the terms of the GNU General Public
#   License when using or distributing such scripts, even though
#   portions of the text of the Macro appear in them. The GNU General
#   Public License (GPL) does govern all other use of the material that
#   constitutes the Autoconf Macro.
#
#   This special exception to the GPL applies to versions of the
#   Autoconf Macro released by the Autoconf Macro Archive. When you
#   make and distribute a modified version of the Autoconf Macro, you
#   may extend this special exception to the GPL to apply to your
#   modified version as well.

AC_DEFUN([CHECK_NCURSES],
[
AC_ARG_WITH([ncurses],
            AS_HELP_STRING([--with-ncurses@<:@=DIR@:>@], [set to ncurses root directory]),
            [
             if test "$withval" = "no"; then
		want_ncurses="no"
             elif test "$withval" = "yes"; then
             want_ncurses="yes"
             ncurses_rootdir=""
             else
	        want_ncurses="yes"
                ncurses_rootdir="$withval"
	     fi
            ],
            [want_ncurses="yes"])
if test "x$want_ncurses" = "xyes"; then
   if test "$ncurses_rootdir" != ""; then
      NCURSES_LIB="-L$ncurses_rootdir/lib -lncurses -lpanel -lmenu -lform"
      NCURSES_H="-I$ncurses_rootdir/include"
   else
      for ncurses_rootdir in /usr /usr/local /usr/share; do
          if test -d "$ncurses_rootdir/include" && test -r "$ncurses_rootdir/include"; then
             NCURSES_LIB="-L$ncurses_rootdir/lib -lncurses -lpanel -lmenu -lform"
             NCURSES_H="-I$ncurses_rootdir/include"
             break;
          fi
      done
   fi
  CPPFLAGS_TMP="$CPPFLAGS"
  CPPFLAGS="$CPPFLAGS $NCURSES_H"
  export CPPFLAGS
   
  LDFLAGS_TMP="$LDFLAGS"
  LDFLAGS="$LDFLAGS $NCURSES_LIB"
  export LDFLAGS
  
  AC_LANG_PUSH(C++)
  AC_CACHE_CHECK([for working ncurses/panel/menu/form], cv_ncurses,
       [AC_TRY_LINK([#include <ncurses.h>
                     #include <panel.h>
                     #include <menu.h>
                     #include <form.h>],
                     [initscr(); update_panels();
                      free_menu(NULL);free_form(NULL)],
                     cv_ncurses=yes, cv_ncurses=no)])
  if test "$cv_ncurses" = yes
     then
       AC_DEFINE([HAVE_NCURSES_H],, [ncurses available])
       NCURSES_OBJ="ncurses.o"
       NCURSES_SRC="examples/ncurses.h examples/ncurses.cpp"
       AC_SUBST(NCURSES_SRC)
       AC_SUBST(NCURSES_OBJ)       
       AC_SUBST(NCURSES_H)
       AC_SUBST(NCURSES_LIB)
     else
       AC_MSG_WARN([ncurses not found])
     fi
 fi
])

AC_DEFUN([CHECK_GTKMM],
[
AC_ARG_WITH([gtkmm],
            AS_HELP_STRING([--with-gtkmm@<:@=DIR@:>@], [set to root directory of gtkmm-x.x.pc (requires pkg-config)]),
            [
             if test "$withval" = "no"; then
		want_gtkmm="no"
             elif test "$withval" = "yes"; then
             want_gtkmm="yes"
             gtkmm_rootdir=""
             else
	        want_gtkmm="yes"
                gtkmm_rootdir="$withval"
	     fi
            ],
            [want_gtkmm="yes"])
if test "x$want_gtkmm" = "xyes"; then
   if test "$gtkmm_rootdir" != ""; then
      export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:$gtkmm_rootdir
   fi
   PKG_PROG_PKG_CONFIG()
   for version in 2.0 2.1 2.2 2.3 2.4;
       do
       PKG_CHECK_EXISTS(gtkmm-$version libglademm-$version, gtkmm_exist="yes", gtkmm_exist="no");
       if test "x$gtkmm_exist" = "xyes"; then
       	  PKG_CHECK_MODULES(GTKMM, gtkmm-$version libglademm-$version)
      	  GTKMM_H="$GTKMM_CFLAGS"
      	  GTKMM_LIB="$GTKMM_LIBS"
      	  GTKMM_OBJ="gtk.o"
	  GTKMM_SRC="examples/gtk.h examples/gtk.cpp"
	  AC_SUBST(GTKMM_SRC)
      	  AC_SUBST(GTKMM_H)
      	  AC_SUBST(GTKMM_LIB)
      	  AC_SUBST(GTKMM_OBJ)
      	  AC_DEFINE([HAVE_GTKMM_H],, [gtkmm is available])
	  break
       fi
   done
   if test "x$gtkmm_exist" = "xno"; then
      AC_MSG_WARN(
[gtkmm/libglademm not found, install them and add directory of gtkmm-x.x.pc/libglademm to your PKG_CONFIG_PATH env variable... Compilation will continue without them]
      	      )
   echo gtkmm-$version glademm-$version
   fi
fi      
])

AC_DEFUN([CHECK_QT],
[
AC_ARG_WITH([qt],
            AS_HELP_STRING([--with-qt@<:@=DIR@:>@], [set to root directory of qt-x.x.pc (requires pkg-config)]),
            [
             if test "$withval" = "no"; then
		want_qt="no"
             elif test "$withval" = "yes"; then
             want_qt="yes"
             qt_rootdir=""
             else
	        want_qt="yes"
                qt_rootdir="$withval"
	     fi
            ],
            [want_qt="yes"])
if test "x$want_qt" = "xyes"; then
   if test "$qt_rootdir" != ""; then
      export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:$qt_rootdir
   fi
   PKG_PROG_PKG_CONFIG()
   PKG_CHECK_EXISTS(QtCore QtGui QtXml, qt_exist="yes", qt_exist="no")
   if test "x$qt_exist" = "xyes"; then
      PKG_CHECK_MODULES(QT, QtCore QtGui QtXml)
      QT_H="$QT_CFLAGS"
      QT_LIB="$QT_LIBS"
      QT_OBJ="gtk.o"
      QT_SRC="examples/gtk.h examples/gtk.cpp"
      AC_SUBST(QT_SRC)
      AC_SUBST(QT_H)
      AC_SUBST(QT_LIB)
      AC_SUBST(QT_OBJ)
      AC_DEFINE([HAVE_QT_H],, [qt is available])
   else
      AC_MSG_WARN(
[qt not found, install qt and add directory of qt-x.x.pc to your PKG_CONFIG_PATH env variable... Compilation will continue without qt]
      	       	       	      	)
   fi
fi      
])